#import "CommentRequest.h"

@interface CommentRequest()
@property (nonatomic,assign)NSString *host;
@property (nonatomic,retain)AFHTTPSessionManager *manager;
@end

CommentRequest *default_comment_request;
// content
@implementation CommentRequest
- (void)initReq:(RequestConfig*)config{
    _host = [config host];
    _manager = [config manager];
}
+ (id)defaultCommentRequest{
    if (default_comment_request == nil) {
        default_comment_request = [[CommentRequest alloc]init];
        [default_comment_request initReq:[RequestConfig defaultConfig]];
    }
    return default_comment_request;
}
-(void)getCommentsByID:(NSString *)contentID success:(void (^)(NSArray<Comment *> *))success failure:(void (^)(NSString *))failure{
    NSString *url = [NSString stringWithFormat:@"%@/comment/%@",_host,contentID];
    [_manager GET:url parameters:nil headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if([responseObject[@"State"] isEqualToString:@"success"]){
            NSMutableArray<Comment*> *list=[[NSMutableArray alloc]init];
            NSArray<NSDictionary*> *comments = responseObject[@"Data"];
            for (NSDictionary* comment in comments) {
                Comment *c = [[Comment alloc]initWith:comment];
                [list addObject:c];
            }
            success(list);
        }else{
            failure(responseObject[@"State"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([NSString stringWithFormat:@"%@",error]);
    }];
}
-(void)deleteCommentsByID:(NSString *)commentID success:(void (^)(void))success failure:(void (^)(NSString *))failure{
    NSString *url = [NSString stringWithFormat:@"%@/comment/%@",_host,commentID];
    [_manager DELETE:url parameters:nil headers:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if([responseObject[@"State"] isEqualToString:@"success"]){
            success();
        }else{
            failure(responseObject[@"State"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([NSString stringWithFormat:@"%@",error]);
    }];
}
-(void)postCommentsByID:(CommentReq *)commentReq success:(void (^)(void))success failure:(void (^)(NSString *))failure{
    NSString *url = [NSString stringWithFormat:@"%@/comment",_host];
    NSDictionary *dict;
    if (commentReq.isReply) {
        dict = @{@"contentId":commentReq.contentId,@"fatherId":commentReq.fatherId,@"content":commentReq.content,@"isReply":@YES};
    }
    else {
        dict = @{@"contentId":commentReq.contentId,@"fatherId":commentReq.fatherId,@"content":commentReq.content,@"isReply":@NO};
    }
    [_manager POST:url parameters:nil headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if([responseObject[@"State"] isEqualToString:@"success"]){
            success();
        }else{
            failure(responseObject[@"State"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([NSString stringWithFormat:@"%@",error]);
    }];
}

@end
